<?php
/*
	批量生成挪车卡二维码
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}else{
		$return['params']['createtime']=array();
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','code','num','status','createtime','remark','show_url');
	$list=pdo_getslice('jy_move_card_create',$params,array($pindex,$psize),$total,$fields,'','id desc');

	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'创建日志'),
			array('op'=>'add','title'=>'批量创建挪车卡'),
		),
		'list'=>array('tip'=>'挪车卡批量创建成功后，可下载挪车二维码交由印刷厂商印刷成挪车卡！也可扫描示例挪车码测试基本流程！','data'=>'list','edit'=>false,'delall'=>false,'del_message'=>'删除后该批次挪车卡将全部失效，确认删除吗？','list'=>array(
			array('name'=>'code','title'=>'批号','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'show_url','title'=>'示例挪车码','type'=>'ewm'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁用','show'=>array('status'=>1)),
			array('type'=>'post','op'=>'start','title'=>'启用','show'=>array('status'=>0)),
			array('type'=>'post','op'=>'download','title'=>'下载挪车二维码'),
		)),
		'search'=>array(
			array('name'=>'createtime','title'=>'创建时间','type'=>'time_range'),
		)
	);
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'禁用',
		'1'=>'正常',
	);
	exi($return);
}
if($_RQ['op']=='download'){
	//下载挪车二维码
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择挪车卡创建批次！','error');
	}
	$item=pdo_get('jy_move_card_create',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('code_file'));
	if(!$item){
		exi('指定批次不存在！','error');
	}
	$url=tomedia($item['code_file']);
	exi('点击确定进入下载页！','','',$url);
}
if($_RQ['op']=='stop'){
	//禁用
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择挪车卡创建批次！','error');
	}
	$item=pdo_get('jy_move_card_create',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('status'));
	if(!$item){
		exi('指定批次不存在！','error');
	}
	if(!$item['status']){
		exi('指定批次已禁用！','status');
	}
	pdo_update('jy_move_card_create',array('status'=>0),array('id'=>$id));
	exi('禁用成功！','','list');
}
if($_RQ['op']=='start'){
	//启用
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择挪车卡创建批次！','error');
	}
	$item=pdo_get('jy_move_card_create',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('status'));
	if(!$item){
		exi('指定批次不存在！','error');
	}
	if($item['status']){
		exi('指定批次已启用！','status');
	}
	pdo_update('jy_move_card_create',array('status'=>1),array('id'=>$id));
	exi('启用成功！','','list');
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$code=pdo_count('jy_move_card_create',array('uniacid'=>$_SESSION['uniacid']));
		
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'code'=>$code+1,
			'num'=>intval($post['num']),
			'status'=>1,
			'seckey'=>random(32),
			'remark'=>trim($post['remark']),
			'createtime'=>TIMESTAMP,
		);
		if(!$insert['num']){
			exi('请先选择创建数量！','error');
		}
		$string=$this->create_card($insert['code'],$insert['num'],$insert['seckey']);
		$string=explode("\n",$string,2);
		$insert['show_url']=$string['0'];
		$string=$string['1'];
		//生成成功后打包成压缩文件
		$temp_file=ROOT_AT.random(30).'.zip';
		$zip=new ZipArchive();
		$zip->open($temp_file,ZipArchive::CREATE);
		$zip->addFromString('url.txt',$string);
		$zip->close();
		$file=cfc('files')->source_save($temp_file,'data','zip','挪车二维码链接文件');
		$insert['code_file']=$file;
		unlink($temp_file);
		pdo_insert('jy_move_card_create',$insert);
		exi('批量创建成功！','','list');
	}
	$return['item']=array();
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'批量创建挪车卡',
		'tabs'=>array(
			array('op'=>'list','title'=>'创建日志'),
			array('op'=>'add','title'=>'批量创建挪车卡'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'remark','title'=>'备注'),
			array('type'=>'string','name'=>'num','title'=>'生成数量'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_move_card_create',array('id'=>$id));	
	}
	
	exi('删除成功！');
}